package com.ydy.service.impl;

import com.ydy.constant.Const;
import com.ydy.enums.Sex;
import com.ydy.mapper.UsersMapper;
import com.ydy.pojo.Users;
import com.ydy.pojo.bo.UserBO;
import com.ydy.resource.ServerUri;
import com.ydy.service.UserService;
import com.ydy.utils.DateUtil;
import com.ydy.utils.MD5Utils;
import com.ydy.utils.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    public static final Logger log =
            LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    public UsersMapper usersMapper;

    @Autowired
    private Sid sid;

    @Autowired
    private ServerUri serverUri;

    /**
     * 查询登录用户名是否存在
     * @Title: queryUsernameIsExist
     * @author: dy.yin 2022/1/12 16:11
     * @param: [userName]    用户登录名称
     * @return: {@link boolean }
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean queryUsernameIsExist(String userName) {
        Users user = new Users();
        user.setUsername(userName);
        Users userResult = usersMapper.selectOne(user);
        log.info("查询用户名称是否存在");
        return userResult == null ? false:true;
    }

    /**
     * 注册用户
     * @Title: registerUser
     * @author: dy.yin 2022/1/12 16:12
     * @param: [users]
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Users registerUser(UserBO userBO){
        Users user = new Users();
        user.setId(sid.next());
        user.setUsername(userBO.getUsername());
        user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));
        // 默认性别为 保密
        user.setSex(Sex.secret.type);
        // 默认用户昵称同用户名
        user.setNickname(userBO.getUsername());
        // 默认生日
        user.setBirthday(DateUtil.stringToDate("1900-01-01"));
        // 默认头像
        user.setFace(Const.FACE_DEFAULT_IMAGE_PATH);
        // 默认生日
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        usersMapper.insertSelective(user);
        log.info("新增用户");
        user.setFace(serverUri.appendImagUrl(Const.FACE_DEFAULT_IMAGE_PATH));
        return user;
    }

    /**
     * 用户登录
     * @Title: queryUserForLogin
     * @author: dy.yin 2022/1/12 16:51
     * @param: [user]  用户信息
     * @return: {@link Users }
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Users queryUserForLogin(UserBO userBO) {
        Users user = new Users();
        user.setUsername(userBO.getUsername());
        user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));
        log.info("用户登录");
        Users users = usersMapper.selectOne(user);
        users.setFace(serverUri.appendImagUrl(users.getFace()));
        return users;
    }
}
